import { computed } from "vue";
import { mapState, useStore, mapGetters, mapActions, mapMutations } from "vuex"

export const useState = (getKeys) => {
    const store = useStore();
    const storeState = {}
    const storeFns = mapState(getKeys);//返回的是个object类型
    Object.keys(storeFns).forEach((fnKeys) => {
        const fn = storeFns[fnKeys].bind({ $store: store })//改变指向
        // storeState[fnKeys] = fn() //计算属性返回值 
        storeState[fnKeys] = computed(fn) //计算属性返回值 
    })
    return storeState
}
export const useGetters = (getKeys) => {
    const store = useStore();
    const storeGetters = {};
    const storeFns = mapGetters(getKeys);//返回的是个object类型
    Object.keys(storeFns).forEach((fnKeys) => {
        const fn = storeFns[fnKeys].bind({ $store: store })
        storeGetters[fnKeys] = computed(fn);
    })
    return storeGetters
}
export const useMutations = (getKeys) => {
    const store = useStore();
    const storeMutations = {};
    const storeFns = mapMutations(getKeys);//返回的是个object类型
    Object.keys(storeFns).forEach((fnKeys) => {
        const fn = storeFns[fnKeys].bind({ $store: store })
        storeMutations[fnKeys] = fn;
    })
    return storeMutations
}
export const useActions = (getKeys) => {
    const store = useStore();
    const storeActions = {};
    const storeFns = mapActions(getKeys);//返回的是个object类型
    Object.keys(storeFns).forEach((fnKeys) => {
        const fn = storeFns[fnKeys].bind({ $store: store })
        storeActions[fnKeys] = fn;
    })
    return storeActions
}
